package com.example.domain;

import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by lenovo on 2018/5/21.
 */

public class FileUtil {


    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        String path = filePath + fileName;
        FileOutputStream out = new FileOutputStream(path);
        out.write(file);
        out.flush();
        out.close();
    }

    public static List<String> getFileList() {
        List<String> fileList = new ArrayList<String>();
        String path = "D:/image/";
        File file = new File(path);        //获取其file对象
        File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
        for (File f : fs) {                    //遍历File[]数组
            if (!f.isDirectory())        //若非目录(即文件)，则打印
            {
                fileList.add(f.getName());
            }
        }
        return fileList;
    }

    public static File getFileByName(String name) {
        String path = "D:/image/";
        File file = new File(path);        //获取其file对象
        File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
        for (File f : fs) {                    //遍历File[]数组
            if (!f.isDirectory())        //若非目录(即文件)，则打印
            {
                String fileName = f.getName();
                if (fileName.equals(name)) {
                    return f;
                }
            }
        }
        return null;
    }

    public static List<String> getCadNames(String path, String projectId) {
        String cadPath = path + "/" + projectId + "/CAD";
        File file = new File(cadPath);
        if (!file.exists()) {
            return null;
        }
        File[] fs = file.listFiles();
        List<String> fileList = new ArrayList<String>();
        for (File f : fs) {
            if (!f.isDirectory()) {
                fileList.add(f.getName());
            }
        }
        return fileList;
    }

    public static List<String> getImgNames(String path, String projectId,String schemaId) {
        String imgPath = path + "/" + projectId + "/IMG/"  + schemaId;
        File file = new File(imgPath);
        if (!file.exists()) {
            return null;
        }
        File[] fs = file.listFiles();
        List<String> fileList = new ArrayList<String>();
        for (File f : fs) {
            if (!f.isDirectory()) {
                fileList.add(f.getName());
            }
        }
        return fileList;
    }
}
